﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Media.Imaging;
using System.Xml;
using System.Xml.Linq;
using Autodesk.Revit.UI;

namespace YArchitech.Public
{
	public class Ribbon
	{
		private XElement MergeItems(IEnumerable<string> xmlFilenames)
		{
			XElement xelement = new XElement("HYRibbon");
			foreach (string text in xmlFilenames)
			{
				if (File.Exists(text))
				{
					try
					{
						XElement xeMerge = XElement.Load(text);
						this.MergeItems(xelement, xeMerge);
					}
					catch
					{
					}
				}
			}
			return xelement;
		}

		private XElement MergeItems(XElement xeAll, XElement xeMerge)
		{
			foreach (XElement xelement in xeMerge.Elements())
			{
				string name = xelement.GetName();
				XElement childElementByName = xeAll.GetChildElementByName(name);
				if (childElementByName != null)
				{
					this.MergeItems(childElementByName, xelement);
				}
				else
				{
					xeAll.Add(xelement);
				}
			}
			return xeAll;
		}

		public List<RibbonPanel> CreateTabs(UIControlledApplication application, string binDir, IEnumerable<string> xmlFilenames, string resDir)
		{
			if (xmlFilenames == null || xmlFilenames.Count<string>() == 0)
			{
				return null;
			}
			List<RibbonPanel> list = new List<RibbonPanel>();
			XElement elem = this.MergeItems(xmlFilenames);
			IEnumerable<XElement> sortedChildElements = elem.GetSortedChildElements();
			if (sortedChildElements == null || sortedChildElements.Count<XElement>() == 0)
			{
				return null;
			}
			foreach (XElement elemTab in sortedChildElements)
			{
				List<RibbonPanel> collection = this.CreateTab(application, binDir, elemTab, resDir);
				if (list != null)
				{
					list.AddRange(collection);
				}
			}
			return list;
		}

		public List<RibbonPanel> CreateTab(UIControlledApplication application, string binDir, XElement elemTab, string resDir)
		{
			if (elemTab == null)
			{
				return null;
			}
			List<RibbonPanel> result;
			try
			{
				string name = elemTab.GetName();
				application.CreateRibbonTab(name);
				CmdTools.listTabName.Add(name);
				List<RibbonPanel> list = new List<RibbonPanel>();
				IEnumerable<XElement> sortedChildElements = elemTab.GetSortedChildElements();
				if (sortedChildElements == null)
				{
					result = null;
				}
				else
				{
					foreach (XElement elemPanel in sortedChildElements)
					{
						RibbonPanel ribbonPanel = this.CreateRibbonPanel(name, application, binDir, resDir, elemPanel);
						if (ribbonPanel != null)
						{
							list.Add(ribbonPanel);
						}
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = null;
			}
			return result;
		}

		public VerticalMenuCommandTableInfos GetHYMenuInfos(IEnumerable<string> xmlFilenames, string binDir, string resDir)
		{
			if (xmlFilenames == null || xmlFilenames.Count<string>() == 0)
			{
				return null;
			}
			XElement elem = this.MergeItems(xmlFilenames);
			IEnumerable<XElement> enumerable = elem.GetSortedChildElements().ToList<XElement>();
			if (enumerable == null || enumerable.Count<XElement>() == 0)
			{
				return null;
			}
			List<VerticalMenuTableInfos> list = new List<VerticalMenuTableInfos>();
			foreach (XElement elemTab in enumerable)
			{
				VerticalMenuTableInfos tableInfos = this.GetTableInfos(elemTab, binDir, resDir);
				if (tableInfos != null)
				{
					list.Add(tableInfos);
				}
			}
			return new VerticalMenuCommandTableInfos(list);
		}

		public VerticalMenuTableInfos GetTableInfos(XElement elemTab, string binDir, string resDir)
		{
			if (elemTab == null)
			{
				return null;
			}
			VerticalMenuTableInfos result;
			try
			{
				string name = elemTab.GetName();
				List<VerticalMenuCommandInfos> list = new List<VerticalMenuCommandInfos>();
				IEnumerable<XElement> sortedChildElements = elemTab.GetSortedChildElements();
				if (sortedChildElements == null)
				{
					result = null;
				}
				else
				{
					foreach (XElement elemPanel in sortedChildElements)
					{
						List<VerticalMenuCommandInfos> commandInfos = this.GetCommandInfos(name, elemPanel, binDir, resDir);
						list.AddRange(commandInfos);
					}
					VerticalMenuTableInfos verticalMenuTableInfos = new VerticalMenuTableInfos(name, list);
					result = verticalMenuTableInfos;
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = null;
			}
			return result;
		}

		public List<VerticalMenuCommandInfos> GetCommandInfos(string tabName, XElement elemPanel, string binDir, string resDir)
		{
			binDir += "\\";
			string buttonIconsFolder = resDir + "\\";
			List<VerticalMenuCommandInfos> result = new List<VerticalMenuCommandInfos>();
			IEnumerable<XElement> sortedChildElements = elemPanel.GetSortedChildElements();
			if (sortedChildElements == null)
			{
				return result;
			}
			foreach (XElement xelement in sortedChildElements)
			{
				string a;
				if ((a = xelement.Name.ToString()) != null)
				{
					if (!(a == "PushButton"))
					{
						if (!(a == "SplitButton"))
						{
							if (!(a == "Stack"))
							{
								continue;
							}
						}
						else
						{
							IEnumerator<XElement> enumerator2 = xelement.Elements().GetEnumerator();
							{
								while (enumerator2.MoveNext())
								{
									XElement node = enumerator2.Current;
									this.GetPushButtonCmdInfos(node, binDir, buttonIconsFolder, ref result);
								}
								continue;
							}
						}
						foreach (XElement xelement2 in xelement.Elements())
						{
							if (xelement2.Name.ToString().CompareTo("PushButton") == 0)
							{
								this.GetPushButtonCmdInfos(xelement2, binDir, buttonIconsFolder, ref result);
							}
						}
					}
					else
					{
						this.GetPushButtonCmdInfos(xelement, binDir, buttonIconsFolder, ref result);
					}
				}
			}
			return result;
		}

		private void GetPushButtonCmdInfos(XElement node, string binDir, string ButtonIconsFolder, ref List<VerticalMenuCommandInfos> commandInfoListWidthPanel)
		{
			string text = node.GetText();
			string attribute = node.GetAttribute("Name");
			string dllName = binDir + node.GetAttribute("DllName");
			string attribute2 = node.GetAttribute("ClassName");
			string imageName = Path.Combine(ButtonIconsFolder, node.GetAttribute("MenuImage"));
			string attribute3 = node.GetAttribute("ToolTip");
			string attribute4 = node.Parent.GetAttribute("Name");
			VerticalMenuCommandInfos item = new VerticalMenuCommandInfos(attribute, text, dllName, attribute2, imageName, attribute3, attribute4);
			commandInfoListWidthPanel.Add(item);
		}

		private bool GetToolTipImage(out string imgFile, XElement node, string resDir)
		{
			imgFile = string.Empty;
			XAttribute xattribute = node.Attribute("ToolTipImage");
			if (xattribute == null)
			{
				return false;
			}
			string text = (string)xattribute;
			if (string.Compare(text, "acs.jpg", true) == 0)
			{
				return false;
			}
			imgFile = Path.Combine(resDir, text);
			return File.Exists(imgFile);
		}

		private void SetToolTipImage(PushButton ribbonItem, XElement node, string resDir)
		{
			string uriString;
			if (this.GetToolTipImage(out uriString, node, resDir))
			{
				ribbonItem.ToolTipImage = new BitmapImage(new Uri(uriString, UriKind.Absolute));
			}
		}

		public RibbonPanel CreateRibbonPanel(string tabName, UIControlledApplication application, string binDir, string resDir, XElement elemPanel)
		{
			binDir += "\\";
			string text = resDir + "\\";
			string name = elemPanel.GetName();
			RibbonPanel ribbonPanel = application.CreateRibbonPanel(tabName, name);
			IEnumerable<XElement> sortedChildElements = elemPanel.GetSortedChildElements();
			if (sortedChildElements == null)
			{
				return ribbonPanel;
			}
			foreach (XElement xelement in sortedChildElements)
			{
                //try
                //{
                //    string key;
                //    if ((key = xelement.Name.ToString()) != null)
                //    {
                //        if (<PrivateImplementationDetails>{74F1EC91-CD59-47A2-A447-1AE9F5FF92BA}.$$method0x60000af-1 == null)
                //        {
                //            <PrivateImplementationDetails>{74F1EC91-CD59-47A2-A447-1AE9F5FF92BA}.$$method0x60000af-1 = new Dictionary<string, int>(8)
                //            {
                //                {
                //                    "TextBox",
                //                    0
                //                },
                //                {
                //                    "PushButton",
                //                    1
                //                },
                //                {
                //                    "ComboBox",
                //                    2
                //                },
                //                {
                //                    "SplitButton",
                //                    3
                //                },
                //                {
                //                    "Separator",
                //                    4
                //                },
                //                {
                //                    "Stack",
                //                    5
                //                },
                //                {
                //                    "RadioButtonGroup",
                //                    6
                //                },
                //                {
                //                    "SlideOut",
                //                    7
                //                }
                //            };
                //        }
                //        int num;
                //        if (<PrivateImplementationDetails>{74F1EC91-CD59-47A2-A447-1AE9F5FF92BA}.$$method0x60000af-1.TryGetValue(key, out num))
                //        {
                //            switch (num)
                //            {
                //            case 0:
                //            {
                //                TextBoxData textBoxData = new TextBoxData(xelement.GetName());
                //                TextBox textBox = (TextBox)ribbonPanel.AddItem(textBoxData);
                //                textBox.Value = xelement.GetAttribute("Value");
                //                textBox.Image = new BitmapImage(new Uri(Path.Combine(text, xelement.GetAttribute("MenuImage")), UriKind.Absolute));
                //                this.SetRibbonItemToolTip(textBox, xelement, tabName, name, resDir);
                //                textBox.ShowImageAsButton = true;
                //                if (xelement.GetAttribute("ShowImageAsButton") == "false")
                //                {
                //                    textBox.ShowImageAsButton = false;
                //                    continue;
                //                }
                //                continue;
                //            }
                //            case 1:
                //            {
                //                PushButtonData pushButtonData = new PushButtonData(xelement.GetName(), xelement.GetText(), binDir + xelement.GetAttribute("DllName"), xelement.GetAttribute("ClassName"));
                //                PushButton pushButton = (PushButton)ribbonPanel.AddItem(pushButtonData);
                //                pushButton.LargeImage = new BitmapImage(new Uri(Path.Combine(text, xelement.GetAttribute("RibbonImage")), UriKind.Absolute));
                //                pushButton.Image = new BitmapImage(new Uri(Path.Combine(text, xelement.GetAttribute("MenuImage")), UriKind.Absolute));
                //                this.SetRibbonItemToolTip(pushButton, xelement, tabName, name, resDir);
                //                continue;
                //            }
                //            case 2:
                //            {
                //                ComboBoxData comboBoxData = new ComboBoxData(xelement.GetName());
                //                ComboBox comboBox = (ComboBox)ribbonPanel.AddItem(comboBoxData);
                //                IEnumerator<XElement> enumerator2 = xelement.Elements().GetEnumerator();
                //                {
                //                    while (enumerator2.MoveNext())
                //                    {
                //                        XElement elem = enumerator2.Current;
                //                        ComboBoxMember comboBoxMember = comboBox.AddItem(new ComboBoxMemberData(elem.GetName(), elem.GetAttribute("Text")));
                //                        comboBoxMember.Image = new BitmapImage(new Uri(Path.Combine(text, elem.GetAttribute("MenuImage")), UriKind.Absolute));
                //                    }
                //                    continue;
                //                }
                //                break;
                //            }
                //            case 3:
                //                break;
                //            case 4:
                //                ribbonPanel.AddSeparator();
                //                continue;
                //            case 5:
                //            {
                //                List<RibbonItemData> list = new List<RibbonItemData>();
                //                if (xelement.Elements().Count<XElement>() == 2)
                //                {
                //                    for (int i = 0; i < 2; i++)
                //                    {
                //                        this.CreateStackControl(xelement, i, list, binDir);
                //                    }
                //                    IList<RibbonItem> ribbonItemsStacked = ribbonPanel.AddStackedItems(list.ElementAt(0), list.ElementAt(1));
                //                    for (int j = 0; j < 2; j++)
                //                    {
                //                        this.UpdateStackControl(tabName, name, xelement, j, ribbonItemsStacked, text);
                //                    }
                //                    continue;
                //                }
                //                if (xelement.Elements().Count<XElement>() == 3)
                //                {
                //                    for (int k = 0; k < 3; k++)
                //                    {
                //                        this.CreateStackControl(xelement, k, list, binDir);
                //                    }
                //                    IList<RibbonItem> ribbonItemsStacked2 = ribbonPanel.AddStackedItems(list.ElementAt(0), list.ElementAt(1), list.ElementAt(2));
                //                    for (int l = 0; l < 3; l++)
                //                    {
                //                        this.UpdateStackControl(tabName, name, xelement, l, ribbonItemsStacked2, text);
                //                    }
                //                    continue;
                //                }
                //                continue;
                //            }
                //            case 6:
                //            {
                //                RadioButtonGroupData radioButtonGroupData = new RadioButtonGroupData(xelement.GetName());
                //                RadioButtonGroup radioButtonGroup = (RadioButtonGroup)ribbonPanel.AddItem(radioButtonGroupData);
                //                IEnumerator<XElement> enumerator3 = xelement.Elements().GetEnumerator();
                //                {
                //                    while (enumerator3.MoveNext())
                //                    {
                //                        XElement elem2 = enumerator3.Current;
                //                        ToggleButton toggleButton = radioButtonGroup.AddItem(new ToggleButtonData(elem2.GetName(), elem2.GetAttribute("Text"), binDir + elem2.GetAttribute("DllName"), elem2.GetAttribute("ClassName")));
                //                        toggleButton.LargeImage = new BitmapImage(new Uri(Path.Combine(text, elem2.GetAttribute("MenuImage")), UriKind.Absolute));
                //                        toggleButton.Image = new BitmapImage(new Uri(Path.Combine(text, elem2.GetAttribute("MenuImage")), UriKind.Absolute));
                //                    }
                //                    continue;
                //                }
                //                goto IL_4B8;
                //            }
                //            case 7:
                //                goto IL_4B8;
                //            default:
                //                continue;
                //            }
                //            this.CreateSplitButton(tabName, ribbonPanel, xelement, binDir, text);
                //            continue;
                //            IL_4B8:
                //            ribbonPanel.AddSlideOut();
                //        }
                //    }
                //}
                //catch (Exception)
                //{
                //}
			}
			return ribbonPanel;
		}

		public RibbonItem GetRibbonItemByName(RibbonPanel panelRibbon, string itemName)
		{
			foreach (RibbonItem ribbonItem in panelRibbon.GetItems())
			{
				if (itemName == ribbonItem.Name)
				{
					return ribbonItem;
				}
			}
			return null;
		}

		public void CreateSplitButton(string tabName, RibbonPanel ribbonSamplePanel, XElement node, string binDir, string ButtonIconsFolder)
		{
			SplitButtonData splitButtonData = new SplitButtonData(node.GetName(), node.GetAttribute("Text"));
			SplitButton splitButton = ribbonSamplePanel.AddItem(splitButtonData) as SplitButton;
			foreach (XElement xelement in node.Elements())
			{
				PushButtonData pushButtonData = new PushButtonData(xelement.GetName(), xelement.GetAttribute("Text"), binDir + xelement.GetAttribute("DllName"), xelement.GetAttribute("ClassName"));
				PushButton pushButton = splitButton.AddPushButton(pushButtonData);
				string attribute = xelement.GetAttribute("RibbonImage");
				if (!string.IsNullOrEmpty(attribute))
				{
					pushButton.LargeImage = new BitmapImage(new Uri(Path.Combine(ButtonIconsFolder, attribute), UriKind.Absolute));
				}
				string attribute2 = xelement.GetAttribute("MenuImage");
				if (!string.IsNullOrEmpty(attribute2))
				{
					pushButton.Image = new BitmapImage(new Uri(Path.Combine(ButtonIconsFolder, attribute2), UriKind.Absolute));
				}
				this.SetRibbonItemToolTip(pushButton, xelement, tabName, ribbonSamplePanel.Name, ButtonIconsFolder);
			}
		}

		public void CreateStackControl(XElement node, int i, List<RibbonItemData> stackList, string binDir)
		{
			XElement xelement = node.Elements().ElementAt(i);
			string text = xelement.Name.ToString();
			string a;
			if ((a = text) != null)
			{
				if (a == "PushButton")
				{
					stackList.Add(new PushButtonData(xelement.GetName(), xelement.GetAttribute("Text"), binDir + xelement.GetAttribute("DllName"), xelement.GetAttribute("ClassName")));
					return;
				}
				if (a == "ComboBox")
				{
					stackList.Add(new ComboBoxData(xelement.GetName()));
					return;
				}
				if (!(a == "TextBox"))
				{
					return;
				}
				stackList.Add(new TextBoxData(xelement.GetName()));
			}
		}

		public void UpdateStackControl(string tabName, string panelName, XElement node, int i, IList<RibbonItem> ribbonItemsStacked, string ButtonIconsFolder)
		{
			XElement xelement = node.Elements().ElementAt(i);
			string text = xelement.Name.ToString();
			string a;
			if ((a = text) != null)
			{
				if (a == "PushButton")
				{
					PushButton pushButton = ribbonItemsStacked[i] as PushButton;
					pushButton.LargeImage = new BitmapImage(new Uri(Path.Combine(ButtonIconsFolder, xelement.GetAttribute("RibbonImage")), UriKind.Absolute));
					pushButton.Image = new BitmapImage(new Uri(Path.Combine(ButtonIconsFolder, xelement.GetAttribute("MenuImage")), UriKind.Absolute));
					this.SetRibbonItemToolTip(pushButton, xelement, tabName, panelName, ButtonIconsFolder);
					return;
				}
				if (!(a == "ComboBox"))
				{
					if (!(a == "TextBox"))
					{
						return;
					}
				}
				else
				{
					if ("selList" == xelement.GetName())
					{
						string text2 = Assembly.GetExecutingAssembly().Location;
						text2 = Path.GetDirectoryName(text2);
						text2 = Path.GetDirectoryName(text2);
						string uriString = text2 + "\\Res\\selList.png";
						ComboBox comboBox = (ComboBox)ribbonItemsStacked[i];
						IEnumerator enumerator = CmdTools.hyGetSelList().GetEnumerator();
						{
							while (enumerator.MoveNext())
							{
								object obj = enumerator.Current;
								XmlNode xmlNode = (XmlNode)obj;
								ComboBoxMember comboBoxMember = comboBox.AddItem(new ComboBoxMemberData(xmlNode.Attributes["Text"].Value, xmlNode.Attributes["Text"].Value));
								comboBoxMember.Image = new BitmapImage(new Uri(uriString, UriKind.Absolute));
							}
							return;
						}
					}
					IEnumerator<XElement> enumerator2 = xelement.Elements().GetEnumerator();
					{
						while (enumerator2.MoveNext())
						{
							XElement elem = enumerator2.Current;
							ComboBox comboBox2 = (ComboBox)ribbonItemsStacked[i];
							ComboBoxMember comboBoxMember2 = comboBox2.AddItem(new ComboBoxMemberData(elem.GetName(), elem.GetAttribute("Text")));
							comboBoxMember2.Image = new BitmapImage(new Uri(Path.Combine(ButtonIconsFolder, elem.GetAttribute("MenuImage")), UriKind.Absolute));
						}
						return;
					}
				}
				TextBox textBox = ribbonItemsStacked[i] as TextBox;
				textBox.Value = xelement.GetAttribute("Value");
				textBox.Image = new BitmapImage(new Uri(Path.Combine(ButtonIconsFolder, xelement.GetAttribute("MenuImage")), UriKind.Absolute));
				this.SetRibbonItemToolTip(textBox, xelement, tabName, panelName, ButtonIconsFolder);
				textBox.ShowImageAsButton = true;
				if (xelement.GetAttribute("ShowImageAsButton") == "false")
				{
					textBox.ShowImageAsButton = false;
				}
			}
		}

		private void SetRibbonItemToolTip(RibbonItem item, XElement xeNode, string tabTitle, string panelTitle, string resDir)
		{
			HYRibbonItemToolTipData hyribbonItemToolTipData = new HYRibbonItemToolTipData
			{
				TabTitle = tabTitle,
				PanelTitle = panelTitle,
				ItemName = item.ItemText,
				Content = xeNode.GetTooltip(),
				ExpandedContent = xeNode.GetExpandedContent()
			};
			string expandedImage;
			if (this.GetToolTipImage(out expandedImage, xeNode, resDir))
			{
				hyribbonItemToolTipData.ExpandedImage = expandedImage;
			}
			string attribute = xeNode.GetAttribute("ExpandedVideo");
			if (!string.IsNullOrEmpty(attribute))
			{
				string text = Path.Combine(resDir, attribute);
				if (File.Exists(text))
				{
					hyribbonItemToolTipData.ExpandedVideo = text;
				}
			}
			CmdTools.HYRibbonItems.Add(HYRibbonItemToolTipData.ConstructKey(hyribbonItemToolTipData), hyribbonItemToolTipData);
		}
	}
}
